
var CurrentUser;    //Global variable

function getName() {
    var user= JSON.parse(localStorage.getItem("loggedinUser"));
    CurrentUser = user;   //global variable
    if (user) {
	    console.log ("Currently logged in:  " + user["name"]);
	    document.getElementById("name-goes-here").innerText= user["name"];
	 } else {
	    console.log ("No one is logged in");
	 }
}
getName(); //run the function

function populateHistory() {

    // find out who is logged in 
    var loggedinUser = JSON.parse(localStorage.getItem("loggedinUser"));  //person who is logged in
    var users = JSON.parse(localStorage.getItem("users"));   //the whole users database
    let email = loggedinUser["email"];                       //the person who is logged in, email

    const user = users.find(u => u.email === email);
    console.log(email);
    console.log(user["history"][0]);

    if (user) {
        console.log("Currently logged in:  " + user["name"]);
        console.log("Currently count is:  " + user["count"]);
        console.log("Current history is " + user["history"][0].date);

        historyArray = user["history"];
        historyArray.forEach((hist) => {
            // Get references to the HTML card and div to hold the history
            let historyCardTemplate = document.getElementById("simple-history-card");
            let historyCardGroup = document.getElementById("history-goes-here");
            let onecard = historyCardTemplate.content.cloneNode(true);

            console.log(hist.date);   //loop through each date
            d = hist.date;
            onecard.querySelector(".info").innerHTML = d;
            historyCardGroup.appendChild(onecard);
        })

    } else {
        console.log("No one is logged in");
    }
}
populateHistory();
